//
//  selectFD.swift
//  swifttest1
//
//  Created by kologe on 2024/9/9.
//

import SwiftUI
import AppKit


struct selectFD: View {
    @Binding var fileName :String
    @State var infileName :String = ""
    @State var fileURL :URL?
    @State private var placeholder: String = "select file"
    
    @State var bClick:Bool = false
    
    var body: some View {
        HStack{
            TextField(placeholder,text:$infileName)
                .padding()
                .textFieldStyle(RoundedBorderTextFieldStyle())
                .disabled(true)
            Button {
                self.bClick = true
                openFilePicker()
            } label: {
                Label("Press Me", systemImage: "")
                    .foregroundColor(.white)
                    .padding(EdgeInsets(top: 4, leading: 4, bottom: 4, trailing: 4))
                    .background(
                        RoundedRectangle(cornerRadius: 10)
                            .foregroundColor(.blue)
                    )
                    .compositingGroup()
                    .shadow(radius: 5,x:0,y:3)
                    .contentShape(Rectangle())
            }
            .nomalStyle()
            .disabled(bClick)
            
            
        }
    }
    
    func openFilePicker() {
        let openPanel = NSOpenPanel()
        openPanel.allowsMultipleSelection = false
        openPanel.canChooseDirectories = true
        openPanel.canChooseFiles = true
        let ret = openPanel.runModal()
        bClick = false
        if ret == .OK {
            if let url = openPanel.url {
                infileName = url.path
                fileName = infileName
            }
        }
    }
}


#Preview {
    selectFD(fileName: .constant("ccc"))
}
